package net.sapfree.core.db;

import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

import net.sapfree.core.util.format.ValueObjectFormat;

public class AccessDataBaseExecutor extends DataBaseExecutor
{
	protected AccessDataBaseExecutor(Connection conn){ super(conn); }

	protected Record fillRecord(ResultSet rs, ResultSetMetaData rsmd, Map<String, String> entryColumnType) throws SQLException 
	{
		Record record = new Record();
//		record.setColumnType(entryColumnType);
		int columnCount = rsmd.getColumnCount();
		for (int i = 1; i <= columnCount; i++)
		{
			Object object = null;
			String columnName = rsmd.getColumnName(i);
			switch(rsmd.getColumnType(i))
			{
				default		:object = rs.getObject(columnName);
							 break;
			}
			record.addData(columnName, object);
		}
		return record;
	}

	protected void setPreparedStatementParameter(PreparedStatement pstmt, int paramIndex, int columnTypeCode, Object paramValue, ValueObjectFormat formatter) throws SQLException
	{
		switch(columnTypeCode)
		{
			//BIT(yes/no,true/false,on/off)  java.lang.Boolean
			case -7:	pstmt.setBoolean(paramIndex, formatter.getJavaBoolean(paramValue));
						break;
			
			//LONGBINARY(ole)   byte[]
			case -4:	InputStream is = formatter.getInputStream(paramValue);
						int iStreamLength = formatter.getLength(paramValue);
						pstmt.setBinaryStream(paramIndex, is, iStreamLength);
						break;
		
			//LONGCHAR(memo) java.lang.String
			case -1:	Reader reader = formatter.getReader(paramValue);
						int iLength = formatter.getLength(paramValue);
						pstmt.setCharacterStream(paramIndex, reader, iLength);
						break;
						
			//DECIMAL(decimal)   java.math.BigDecimal
			case 2:		pstmt.setBigDecimal(paramIndex, formatter.getBigDecimal(paramValue));
						break;
						
			//INTEGER(long,counter)  java.lang.Integer
			case 4:		pstmt.setInt(paramIndex, formatter.getInt(paramValue));
						break;
			
			//SMALLINT(int)	java.lang.Short
			case 5:		pstmt.setShort(paramIndex, formatter.getShort(paramValue));
						break;
			
			//REAL(float) java.lang.Float
			case 7:		pstmt.setFloat(paramIndex, formatter.getFloat(paramValue));
						break;

			//DOUBLE(double) java.lang.Double
			case 8:		pstmt.setDouble(paramIndex, formatter.getDouble(paramValue));
						break;
			
			//VARCHAR(text) java.lang.String
			case 12:	pstmt.setString(paramIndex, (String)paramValue);
						break;

			//DATETIME(datetime,date(l/m/s),time(l/m/s))  java.sql.Timestamp
			case 93:	pstmt.setTimestamp(paramIndex, formatter.getJavaSqlTimestamp(paramValue,null));
						break;

			default:    throw new RuntimeException("one oracle column type in used is not supported! you can use the following type : CHAR/NUMBER/VARCHAR2/DATE/TIMESTAMP/BLOB/CLOB.");
		}
	}
}
//-------------------------
//COL_TEXT
//12
//VARCHAR
//java.lang.String
//-------------------------
//COL_MEMO
//-1
//LONGCHAR
//java.lang.String
//-------------------------
//COL_INT
//5
//SMALLINT
//java.lang.Short
//-------------------------
//COL_LONG
//4
//INTEGER
//java.lang.Integer
//-------------------------
//COL_FLOAT
//7
//REAL
//java.lang.Float
//-------------------------
//COL_DOUBLE
//8
//DOUBLE
//java.lang.Double
//-------------------------
//COL_DOT
//2
//DECIMAL
//java.math.BigDecimal
//-------------------------
//COL_DATE_TIME
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_DATE_L
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_DATE_M
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_DATE_S
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_TIME_L
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_TIME_M
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_TIME_S
//93
//DATETIME
//java.sql.Timestamp
//-------------------------
//COL_AUTO_INC
//4
//COUNTER
//java.lang.Integer
//-------------------------
//COL_YES_NO
//-7
//BIT
//java.lang.Boolean
//-------------------------
//COL_TRUE_FALSE
//-7
//BIT
//java.lang.Boolean
//-------------------------
//COL_ON_OFF
//-7
//BIT
//java.lang.Boolean
//-------------------------
//COL_OLE
//-4
//LONGBINARY
//[B
